$(function(){
    function CheckUserName() {
        if ($('#loginname').val() != "") {
            if ($('#password').val() != "") {
                if($("#verifcode").val() != ""){
                    $.ajax({
                        url : base+"/doLogin",
                        data : jQuery("#loginfrom").serialize(),
                        type : "POST",
                        async: false,
                        success : function(res) {
                            if (res == "true") {
                                //$(location).attr('href', base+"/resource/index");
                                window.location.href = base+"/resource/index";
                            } else {
                                alert(res);
                            }
                        },
                        fail : function(res) {
                            layer.msg('系统错误！', {
                                icon : 5
                            });
                        }
                    });
                    return false;
                }else{
                    layer.msg('验证码不能为空！', {
                        icon : 5
                    });
                    $('#verifcode').focus();
                    return false;
                }
            } else {
                layer.msg('密码不能为空！', {
                    icon : 5
                });
                $('#password').focus();
                return false;
            }

        } else {
            layer.msg('用户名不能为空！', {
                icon : 5
            });
            $('#loginname').focus();
            return false;
        }
    }

    //切换验证码
    function next_captcha() {
        $(this).attr("src", base+"/captcha/next?_=" + new Date().getTime());
    }


    $(document).keydown(function(event){
        if(event.keyCode==13){
            CheckUserName();
        }
    });

    $("button").click(CheckUserName);
    $("#captcha_img").click(next_captcha);

});